package com.finance.model.dishonestPeople;

import com.alibaba.fastjson.JSONObject;
import com.finance.model.dishonestPeople.dto.DishonestPeopleRequestDto;
import com.finance.model.dishonestPeople.dto.DishonestPeopleResponseDto;
import com.finance.pojo.CertificationInfo;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author: Apollo
 * @Date: 2021/4/18
 * @Description: 失信人员
 */
@Service
public class DishonestPeople {

    @Resource
    private OkHttpClient okHttpClient;


    /**
     * 查询失信人员
     * @param certificationInfo
     */
    public DishonestPeopleResponseDto searchForDishonestPersons(CertificationInfo certificationInfo) throws IOException {
        //先创建请求构造器
        Request.Builder builder = new Request.Builder();
        DishonestPeopleRequestDto dishonestPeopleRequestDto = new DishonestPeopleRequestDto();
        dishonestPeopleRequestDto.setIdcard(certificationInfo.getIdNumber());
        dishonestPeopleRequestDto.setName(certificationInfo.getName());
        //构造路径
        builder.url(DishonestPeopleRequestDto.SERVICE_URL+"?"+ DishonestPeopleRequestDto.COL_ID_CARD+"="+dishonestPeopleRequestDto.getIdcard()+"&"+ DishonestPeopleRequestDto.COL_NAME+"="+dishonestPeopleRequestDto.getName());
        builder.get();
        //构造头部内容
        builder.header(DishonestPeopleRequestDto.AUTHORIZATION_HEADER_KEY, DishonestPeopleRequestDto.APP_CODE_PRE+ DishonestPeopleRequestDto.APP_CODE);
        Request request = builder.build();
        //客户端执行
        Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        //处理json字符串
        String jsonString = response.body().string();
        DishonestPeopleResponseDto dishonestPeopleResponseDto = JSONObject.parseObject(jsonString, DishonestPeopleResponseDto.class);
        System.out.println(dishonestPeopleResponseDto);
        return dishonestPeopleResponseDto;
    }





//    public static void main(String[] args) throws IOException {
//        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
//        annotationConfigApplicationContext.scan("ocr.wyk.dto");
//        //查询失信人员
////        searchForDishonestPersons();
//    }






//    /**
//     * 查询失信人员
//     */
//    private static DishonestPeopleResponseDto searchForDishonestPersons() throws IOException {
//        OkHttpClient okHttpClient = new OkHttpClient();
//        Request.Builder builder = new Request.Builder();
//        DishonestPeopleRequestDto dishonestPeopleRequestDto = new DishonestPeopleRequestDto();
//        dishonestPeopleRequestDto.setIdcard("410922200108160613");
//        dishonestPeopleRequestDto.setName("王岩康");
//        builder.url(DishonestPeopleRequestDto.SERVICE_URL+"?"+DishonestPeopleRequestDto.COL_ID_CARD+"="+dishonestPeopleRequestDto.getIdcard()+"&"+DishonestPeopleRequestDto.COL_NAME+"="+dishonestPeopleRequestDto.getName());
//        builder.get();
//        builder.header(DishonestPeopleRequestDto.AUTHORIZATION_HEADER_KEY,DishonestPeopleRequestDto.APP_CODE_PRE+DishonestPeopleRequestDto.APP_CODE);
//        Request request = builder.build();
//        Call call = okHttpClient.newCall(request);
//        Response response = call.execute();
//        String jsonString = response.body().string();
//        DishonestPeopleResponseDto dishonestPeopleResponseDto = JSONObject.parseObject(jsonString, DishonestPeopleResponseDto.class);
//        System.out.println(dishonestPeopleResponseDto);
//        return dishonestPeopleResponseDto;
//    }


}
